package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.operate.trading.api.parameter.BatchAcceptOrderBean;
import com.ymatou.operate.trading.api.service.BatchAcceptOrderCall;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.constant.ECode;

/**
 * 批量接单 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_BatchAcceptOrder {

	private static BatchAcceptOrderBean batchacceptorderbean;

	private static BatchAcceptOrderCall batchacceptordercall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量接单");
	}

	@Before
	public void caseUp() {

		batchacceptorderbean = new BatchAcceptOrderBean();
		batchacceptordercall = new BatchAcceptOrderCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchacceptordercall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_BatchAcceptOrder_001() {
		Logger.start(true, "批量接单成功");
		try {
			Logger.comment("前置数据准备");
			ArrayList<Integer> list = new ArrayList<Integer>();
			Map map = new OrderCallService().createBatchM2cOrder(3);
			JSONArray OrderIds = (JSONArray) map.get("orderIds");
			for (int i = 0; i < OrderIds.length(); i++) {
				list.add(OrderIds.getInt(i));
			}
			// 支付订单
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			// 计算所有订单支付定金
			BigDecimal totalPrice = new OrderCallService()
					.getNeedPrePayAmountOfCashBymainOrderId(tradingId);
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			Logger.comment("前置数据准备结束");
			// 参数
			batchacceptorderbean.setOrderIds(list);
			batchacceptorderbean.setSellerId(sellerId);
			// 发送请求
			batchacceptordercall.setData(batchacceptorderbean);
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchacceptordercall.getCode(),
					"验证Code");
			Logger.verifyEquals(true, batchacceptordercall
					.getAcceptOrderResultList().get(0).getIsSuccess(),
					"验证第一个订单接单状态");
			// 数据库验证
			Thread.sleep(4000);
			DefaultWapper database = new DefaultWapper();
			HashMap<String, Integer> m = new HashMap<String, Integer>();
			for (int OrderId : list) {
				m.put("OrderId", OrderId);
				m.put("TradingStatus", 17);
				m.put("TraddingId", OrderId);
				m.put("SellerId", sellerId);
				Logger.verifyEquals(1, database.selectOrder(m).size(),
						"验证订单表记录");
				Logger.verifyEquals(1, database.selectTradingStatus(m).size(),
						"验证交易表记录");
				// Logger.verifyEquals(1, database.selectTradingNotifaction(m).size(), "验证交易通知表");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchAcceptOrder_002() {
		Logger.start(true, "此订单不存在");
		try {
			ArrayList<Integer> list = new ArrayList<Integer>();
			list.add(-123);
			// int OrderId = OrderIds.getInt(0);
			int SellerId = 288484;
			// 参数
			batchacceptorderbean.setOrderIds(list);
			batchacceptorderbean.setSellerId(SellerId);
			// 发送请求
			batchacceptordercall.setData(batchacceptorderbean);
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchacceptordercall.getCode(),
					"验证Code");
			String info0 = batchacceptordercall.getAcceptOrderResultList()
					.get(0).getResultInfo();
			Logger.verifyEquals(true, info0.contains(ECode.FAILED201_MSG_11),
					"验证AcceptOrderResultList:ResultInfo包含字段："
							+ ECode.FAILED201_MSG_11);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchAcceptOrder_003() {
		Logger.start(true, "订单状态未支付不能确认接单");
		try {
			Logger.comment("前置数据准备");
			ArrayList<Integer> list = new ArrayList<Integer>();
			Map map = new OrderCallService().createBatchM2cOrder(3);
			JSONArray OrderIds = (JSONArray) map.get("orderIds");
			for (int i = 0; i < OrderIds.length(); i++) {
				list.add(OrderIds.getInt(i));
			}
			// int OrderId = OrderIds.getInt(0);
			int SellerId = (Integer) map.get("sellerId");
			Logger.comment("前置数据准备结束");
			// 支付订单
			/*
			 * BigDecimal TotalPrice = (BigDecimal) map.get("TotalPrice"); int TradingId = (Integer)
			 * map.get("TradingId"); new OrderCallService().buyerOrderPay(TotalPrice, TradingId);
			 */
			// 参数
			batchacceptorderbean.setOrderIds(list);
			batchacceptorderbean.setSellerId(SellerId);
			// 发送请求
			batchacceptordercall.setData(batchacceptorderbean);
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchacceptordercall.getCode(),
					"验证Code");
			String info0 = batchacceptordercall.getAcceptOrderResultList()
					.get(0).getResultInfo();
			Logger.verifyEquals(true, info0.contains(ECode.FAILED201_MSG_49),
					"验证AcceptOrderResultList:ResultInfo包含字段："
							+ ECode.FAILED201_MSG_11);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchAcceptOrder_004() {
		Logger.start(true, "操作id为空");
		try {
			Logger.comment("前置数据准备");
			ArrayList<Integer> list = new ArrayList<Integer>();
			Map map = new OrderCallService().createBatchM2cOrder(3);
			JSONArray OrderIds = (JSONArray) map.get("orderIds");
			for (int i = 0; i < OrderIds.length(); i++) {
				list.add(OrderIds.getInt(i));
			}
			// int OrderId = OrderIds.getInt(0);
			int SellerId = (Integer) map.get("sellerId");
			Logger.comment("前置数据准备结束");
			// 支付订单
			/*
			 * BigDecimal TotalPrice = (BigDecimal) map.get("TotalPrice"); int TradingId = (Integer)
			 * map.get("TradingId"); new OrderCallService().buyerOrderPay(TotalPrice, TradingId);
			 */
			// 参数
			batchacceptorderbean.setOrderIds(list);
			// batchacceptorderbean.setSellerId(SellerId);
			// 发送请求
			batchacceptordercall.setData(batchacceptorderbean);
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.ERROR400, batchacceptordercall.getCode(),
					"验证Code");
			Logger.verifyEquals(ECode.ERROR400_MSG_12,
					batchacceptordercall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchAcceptOrder_005() {
		Logger.start(true, "操作id为空");
		try {
			Logger.comment("前置数据准备");
			ArrayList<Integer> list = new ArrayList<Integer>();
			Map map = new OrderCallService().createBatchM2cOrder(3);
			JSONArray OrderIds = (JSONArray) map.get("orderIds");
			for (int i = 0; i < OrderIds.length(); i++) {
				list.add(OrderIds.getInt(i));
			}
			// int OrderId = OrderIds.getInt(0);
			int SellerId = (Integer) map.get("sellerId");
			Logger.comment("前置数据准备结束");
			/*
			 * // 支付订单 BigDecimal totalPrice = (BigDecimal) map.get("TotalPrice"); int tradingId =
			 * (Integer)map.get("TradingId"); new OrderCallService().orderPayRequest(userId,
			 * totalPrice, tradingId);
			 */
			// 参数
			batchacceptorderbean.setOrderIds(list);
			// batchacceptorderbean.setSellerId(SellerId);
			// 发送请求
			batchacceptordercall.setData(batchacceptorderbean);
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.ERROR400, batchacceptordercall.getCode(),
					"验证Code");
			Logger.verifyEquals(ECode.ERROR400_MSG_12,
					batchacceptordercall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchAcceptOrder_006() {
		Logger.start(true, "批量重复接单成功");
		try {
			Logger.comment("前置数据准备");
			ArrayList<Integer> list = new ArrayList<Integer>();
			Map map = new OrderCallService().createBatchM2cOrder(3);
			JSONArray OrderIds = (JSONArray) map.get("orderIds");
			for (int i = 0; i < OrderIds.length(); i++) {
				list.add(OrderIds.getInt(i));
			}
			// 支付订单
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			// 计算所有订单支付定金
			BigDecimal totalPrice = new OrderCallService()
					.getNeedPrePayAmountOfCashBymainOrderId(tradingId);
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			Logger.comment("前置数据准备结束");
			// 参数
			batchacceptorderbean.setOrderIds(list);
			batchacceptorderbean.setSellerId(sellerId);
			// 发送请求
			batchacceptordercall.setData(batchacceptorderbean);
			batchacceptordercall.callService();
			Logger.comment("批量重复接单");
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchacceptordercall.getCode(),
					"验证Code");
			Logger.verifyEquals(true, batchacceptordercall
					.getAcceptOrderResultList().get(0).getIsSuccess(),
					"验证第一个订单接单状态");
			// 数据库验证
			Thread.sleep(4000);
			DefaultWapper database = new DefaultWapper();
			HashMap<String, Integer> m = new HashMap<String, Integer>();
			for (int OrderId : list) {
				m.put("OrderId", OrderId);
				m.put("TradingStatus", 17);
				m.put("TraddingId", OrderId);
				m.put("SellerId", sellerId);
				Logger.verifyEquals(1, database.selectOrder(m).size(),
						"验证订单表记录");
				Logger.verifyEquals(1, database.selectTradingStatus(m).size(),
						"验证交易表记录");
				// Logger.verifyEquals(1, database.selectTradingNotifaction(m).size(), "验证交易通知表");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchAcceptOrder_007() {
		Logger.start(true, "订单状态为风控中,批量接单失败");
		try {
			Logger.comment("前置数据准备");
			ArrayList<Integer> list = new ArrayList<Integer>();
			Map map = new OrderCallService().createBatchM2cOrder(3);
			JSONArray OrderIds = (JSONArray) map.get("orderIds");
			for (int i = 0; i < OrderIds.length(); i++) {
				list.add(OrderIds.getInt(i));
			}
			// 支付订单
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			// 计算所有订单支付定金
			BigDecimal totalPrice = new OrderCallService()
					.getNeedPrePayAmountOfCashBymainOrderId(tradingId);
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 模拟风控
			YmatouEasyCall RiskControlNotifyCall = new YmatouEasyCall("",
					"POST", "JSON");
			for (int i = 0; i < OrderIds.length(); i++) {
				int orderId = OrderIds.getInt(i);
				String RiskControlNotifyString = "{OrderId:" + orderId
						+ ",ParamExt:{\"Status\":\"Auditing\"}}";
				RiskControlNotifyCall
						.setUrl("http://api.trading.operate.ymatou.com/api/Notify/RiskControlNotify");
				RiskControlNotifyCall.setData(RiskControlNotifyString);
				Logger.comment("风控检测");
				RiskControlNotifyCall.callService();
			}
			Logger.comment("前置数据准备结束");
			// 参数
			batchacceptorderbean.setOrderIds(list);
			batchacceptorderbean.setSellerId(sellerId);
			// 发送请求
			batchacceptordercall.setData(batchacceptorderbean);
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchacceptordercall.getCode(),
					"验证Code");
			for (int i = 0; i < OrderIds.length(); i++) {
				int orderId = OrderIds.getInt(i);
				Logger.verifyEquals(orderId, batchacceptordercall
						.getAcceptOrderResultList().get(i).getOrderId(), "验证第"
						+ (i + 1) + "个订单号");
				Logger.verifyEquals(false, batchacceptordercall
						.getAcceptOrderResultList().get(i).getIsSuccess(),
						"验证第" + (i + 1) + "个订单接单状态");
				Logger.verifyEquals(true, batchacceptordercall
						.getAcceptOrderResultList().get(i).getResultInfo()
						.contains(ECode.FAILED201_MSG_56), "验证第" + (i + 1)
						+ "个订单接单返回信息");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchAcceptOrder_008() {
		Logger.start(true, "订单状态为风控不通过,批量接单失败");
		try {
			Logger.comment("前置数据准备");
			ArrayList<Integer> list = new ArrayList<Integer>();
			Map map = new OrderCallService().createBatchM2cOrder(3);
			JSONArray OrderIds = (JSONArray) map.get("orderIds");
			for (int i = 0; i < OrderIds.length(); i++) {
				list.add(OrderIds.getInt(i));
			}
			// 支付订单
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			DecimalFormat df = new DecimalFormat("0.00");// 保留两位小数
			BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 模拟风控
			YmatouEasyCall RiskControlNotifyCall = new YmatouEasyCall("",
					"POST", "JSON");
			for (int i = 0; i < OrderIds.length(); i++) {
				int orderId = OrderIds.getInt(i);
				String RiskControlNotifyString = "{OrderId:" + orderId
						+ ",ParamExt:{\"Status\":\"Rejected\"}}";
				RiskControlNotifyCall
						.setUrl("http://api.trading.operate.ymatou.com/api/Notify/RiskControlNotify");
				RiskControlNotifyCall.setData(RiskControlNotifyString);
				Logger.comment("风控检测");
				RiskControlNotifyCall.callService();
			}
			Logger.comment("前置数据准备结束");
			// 参数
			batchacceptorderbean.setOrderIds(list);
			batchacceptorderbean.setSellerId(sellerId);
			// 发送请求
			batchacceptordercall.setData(batchacceptorderbean);
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchacceptordercall.getCode(),
					"验证Code");
			for (int i = 0; i < OrderIds.length(); i++) {
				int orderId = OrderIds.getInt(i);
				Logger.verifyEquals(orderId, batchacceptordercall
						.getAcceptOrderResultList().get(i).getOrderId(), "验证第"
						+ (i + 1) + "个订单号");
				Logger.verifyEquals(false, batchacceptordercall
						.getAcceptOrderResultList().get(i).getIsSuccess(),
						"验证第" + (i + 1) + "个订单接单状态");
				Logger.verifyEquals(true, batchacceptordercall
						.getAcceptOrderResultList().get(i).getResultInfo()
						.contains(ECode.FAILED201_MSG_49), "验证第" + (i + 1)
						+ "个订单接单返回信息");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchAcceptOrder_009() {
		Logger.start(true, "订单状态为风控通过,批量接单成功");
		try {
			Logger.comment("前置数据准备");
			ArrayList<Integer> list = new ArrayList<Integer>();
			Map map = new OrderCallService().createBatchM2cOrder(3);
			JSONArray OrderIds = (JSONArray) map.get("orderIds");
			for (int i = 0; i < OrderIds.length(); i++) {
				list.add(OrderIds.getInt(i));
			}
			// 支付订单
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			// 计算所有订单支付定金
			BigDecimal totalPrice = new OrderCallService()
					.getNeedPrePayAmountOfCashBymainOrderId(tradingId);
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 模拟风控
			YmatouEasyCall RiskControlNotifyCall = new YmatouEasyCall("",
					"POST", "JSON");
			for (int i = 0; i < OrderIds.length(); i++) {
				int orderId = OrderIds.getInt(i);
				String RiskControlNotifyString = "{OrderId:" + orderId
						+ ",ParamExt:{\"Status\":\"Pass\"}}";
				RiskControlNotifyCall
						.setUrl("http://api.trading.operate.ymatou.com/api/Notify/RiskControlNotify");
				RiskControlNotifyCall.setData(RiskControlNotifyString);
				Logger.comment("风控检测");
				RiskControlNotifyCall.callService();
			}
			Logger.comment("前置数据准备结束");
			// 参数
			batchacceptorderbean.setOrderIds(list);
			batchacceptorderbean.setSellerId(sellerId);
			// 发送请求
			batchacceptordercall.setData(batchacceptorderbean);
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchacceptordercall.getCode(),
					"验证Code");
			for (int i = 0; i < OrderIds.length(); i++) {
				int orderId = OrderIds.getInt(i);
				Logger.verifyEquals(orderId, batchacceptordercall
						.getAcceptOrderResultList().get(i).getOrderId(), "验证第"
						+ (i + 1) + "个订单号");
				Logger.verifyEquals(true, batchacceptordercall
						.getAcceptOrderResultList().get(i).getIsSuccess(),
						"验证第" + (i + 1) + "个订单接单状态");
				Logger.verifyEquals(null, batchacceptordercall
						.getAcceptOrderResultList().get(i).getResultInfo(),
						"验证第" + (i + 1) + "个订单接单返回信息");
			}
			// 数据库验证
			Thread.sleep(4000);
			DefaultWapper database = new DefaultWapper();
			HashMap<String, Integer> m = new HashMap<String, Integer>();
			for (int OrderId : list) {
				m.put("OrderId", OrderId);
				m.put("TradingStatus", 17);
				m.put("TraddingId", OrderId);
				m.put("SellerId", sellerId);
				Logger.verifyEquals(1, database.selectOrder(m).size(),
						"验证订单表记录");
				Logger.verifyEquals(1, database.selectTradingStatus(m).size(),
						"验证交易表记录");
				// Logger.verifyEquals(1, database.selectTradingNotifaction(m).size(), "验证交易通知表");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_BatchAcceptOrder_010() {
		Logger.start(true, "批量订单中有不能确认接单状态,批量接单返回结果正确");
		try {
			Logger.comment("前置数据准备");
			// 创建订单
			Logger.comment("准备前置数据");

			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			int orderId1 = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 计算所有订单支付定金
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 第二单
			PlaceOrderResp placeOrderResp2 = new OrderCallService()
					.createM2cOrder();

			int orderId2 = placeOrderResp2.getOrderList().get(0).getOrderId();
			int tradingId2 = placeOrderResp2.getTradingId();

			BigDecimal totalPrice2 = placeOrderResp2.getPayAmount();// 支付金额

			Logger.comment("前置数据准备结束");
			ArrayList<Integer> list = new ArrayList<Integer>();
			list.add(orderId1);
			list.add(orderId2);
			// 参数
			batchacceptorderbean.setOrderIds(list);
			batchacceptorderbean.setSellerId(sellerId);
			// 发送请求
			batchacceptordercall.setData(batchacceptorderbean);
			batchacceptordercall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchacceptordercall.getCode(),
					"验证Code");
			Logger.verifyEquals(orderId1, batchacceptordercall
					.getAcceptOrderResultList().get(0).getOrderId(), "验证订单号:"
					+ orderId1);
			Logger.verifyEquals(true, batchacceptordercall
					.getAcceptOrderResultList().get(0).getIsSuccess(), "验证订单号:"
					+ orderId1 + "接单状态");
			Logger.verifyEquals(null, batchacceptordercall
					.getAcceptOrderResultList().get(0).getResultInfo(), "验证订单号"
					+ orderId1 + "接单返回信息");

			Logger.verifyEquals(orderId2, batchacceptordercall
					.getAcceptOrderResultList().get(1).getOrderId(), "验证订单号:"
					+ orderId2);
			Logger.verifyEquals(false, batchacceptordercall
					.getAcceptOrderResultList().get(1).getIsSuccess(), "验证订单号:"
					+ orderId2 + "接单状态");
			Logger.verifyEquals(true, batchacceptordercall
					.getAcceptOrderResultList().get(1).getResultInfo()
					.contains(ECode.FAILED201_MSG_49), "验证订单号" + orderId2
					+ "接单返回信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
